//
//  MJ_TaskCellLayout.m
//  UEAssistant
//
//  Created by mac on 2018/1/2.
//  Copyright © 2018年 youyixinxi. All rights reserved.
//

#import "MJ_TaskCellLayout.h"
#import "Gallop.h"
#import "UIColor+Extend.h"
#import "UIImage+Color.h"
#define RowHeight 16

@implementation MJ_TaskCellLayout

- (id)copyWithZone:(NSZone *)zone {
    
    MJ_TaskCellLayout *layout = [[MJ_TaskCellLayout alloc] init];
    layout.cellHeight = self.cellHeight;
    layout.resultModel = [self.resultModel copy];
    layout.isHistoryOrder = self.isHistoryOrder;
    return layout;
}

- (instancetype)initWithModel:(MJ_TaskListResult *)model type:(MJTaskOrder)type isHistory:(BOOL)isHistory
{
    self = [super init];
    if (self) {
        self.resultModel = model;
        self.isHistoryOrder = isHistory;
        LWImageStorage *roundBlue = [[LWImageStorage alloc] init];
        roundBlue.frame = CGRectMake(10, 18, 0, 0);
        
        //-------------------------标题--------------------------------//
        LWTextStorage *titleStorage = [[LWTextStorage alloc] init];
        titleStorage.frame = CGRectMake(roundBlue.right + 8, 18, Screen_Width/3.0*2.0, 15);
        titleStorage.textColor = [UIColor colorWithHexString:@"#333333"];
        titleStorage.text = model.project_name == nil ? model.projectName:model.project_name;
        titleStorage.font = [UIFont fontWithName:@"Helvetica-Bold"size:15];
        [self addStorage:titleStorage];
        
        //-------------------------语种-------------------------------------//
        //语种icon
        LWImageStorage *languageIcon = [[LWImageStorage alloc] init];
        languageIcon.contents = [UIImage imageNamed:@"语种"];
        languageIcon.frame = CGRectMake(18, titleStorage.bottom+12, RowHeight, RowHeight);
        [self addStorage:languageIcon];
        //语种内容
        LWTextStorage *languageTranInfo = [[LWTextStorage alloc] init];
        languageTranInfo.frame = CGRectMake(43, languageIcon.top, Screen_Width-106, RowHeight);
        languageTranInfo.textColor = [UIColor colorWithHexString:@"#666666"];
        languageTranInfo.font = [UIFont systemFontOfSize:14];
        NSString *language_One = [NSString stringWithFormat:@"%@ → %@",model.sourceLangValue,model.targetLangValue];
        NSString *language_Two = [NSString stringWithFormat:@"%@ → %@",model.sourcelangname,model.targetlangname];
        languageTranInfo.text = model.sourceLangValue == nil ? language_Two:language_One;
        [self addStorage:languageTranInfo];

        
        //--------------------------------截止时间-------------------------------//
        //截止时间icon
        LWImageStorage *deadlineTimeIcon = [[LWImageStorage alloc] init];
        deadlineTimeIcon.contents = [UIImage imageNamed:@"截止时间"];
        deadlineTimeIcon.frame = CGRectMake(18, languageIcon.bottom+12, languageIcon.width, languageIcon.height);
        [self addStorage:deadlineTimeIcon];
        
        //截止时间
        LWTextStorage *deadTime = [[LWTextStorage alloc] init];
        deadTime.frame = CGRectMake(43, deadlineTimeIcon.top, Screen_Width-61, RowHeight);
        deadTime.text = model.deadLineTime == nil ? model.deadline_time:model.deadLineTime;
        deadTime.textColor = [UIColor colorWithHexString:@"#666666"];
        deadTime.font = [UIFont systemFontOfSize:14];
        [self addStorage:deadTime];

        //----------------------右边翻译，编辑，校对操作环节tag image-----------------//
        NSDictionary *imageDictionary = @{@"翻译":@"翻译",
                                          @"编辑":@"编辑",
                                          @"校对":@"校对",
                                          @"1":@"翻译",
                                          @"2":@"编辑",
                                          @"3":@"校对"};
        LWImageStorage *opeartionTagImageStorage = [[LWImageStorage alloc] init];
        NSString *taskType = model.tasktype == nil ? model.taskType:model.tasktype;
        NSString *imageStr = imageDictionary[taskType];
        if (isHistory) {//如果为历史订单，则显示为灰色图标
            imageStr = [imageStr stringByAppendingString:@"_pre"];
        }
        opeartionTagImageStorage.contents = [UIImage imageNamed:imageStr];
        opeartionTagImageStorage.frame = CGRectMake(Screen_Width-20-Radio*37, 0, Radio*37, Radio*37);
        opeartionTagImageStorage.contentMode = UIViewContentModeScaleAspectFit;
        [self addStorage:opeartionTagImageStorage];
        
        //---------------------------------字数统计-------------------------------//
        LWTextStorage *wordCountLabel = [[LWTextStorage alloc] init];
        NSString *wordText = [NSString stringWithFormat:@"%.0f字",(model.wordCount == 0 ? model.wordcount:model.wordCount)];
        CGFloat wordTextWidth = [self calulateTextHeightWithString:wordText fontSize:14];
        wordCountLabel.frame = CGRectMake(Screen_Width-wordTextWidth-18, opeartionTagImageStorage.bottom+12, wordTextWidth, RowHeight);
        wordCountLabel.text = wordText;
        wordCountLabel.maxNumberOfLines = 1;
        wordCountLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        wordCountLabel.font = [UIFont systemFontOfSize:14];
        [self addStorage:wordCountLabel];
        
                //-------------------如果为平台则添加显示单价----------------------------//
        if (type == MJTaskOrderWaitWithPlat || type == MJTaskOrderOngoingWithPlat) {
            UIColor *color = isHistory?[UIColor colorWithHexString:@"#999999"]:[UIColor redColor];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:model.price attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:color}];
            [str appendAttributedString:[[NSAttributedString alloc] initWithString:@"元/千字" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]}]];
            CGRect frame = CGRectMake(languageTranInfo.right, wordCountLabel.bottom+8, Screen_Width-languageTranInfo.right-15, RowHeight);
            LWTextStorage *priceLabel = [LWTextStorage lw_textStorageWithText:str frame:frame];
            priceLabel.textAlignment = NSTextAlignmentRight;
            [self addStorage:priceLabel];
        }
        
        //---------------------------------标签-------------------------------//
        NSString *typeStr = model.labels == nil ? model.label_idname_list:model.labels;
        NSArray *types = [typeStr componentsSeparatedByString:@","];
        if (model.labels != nil) {
            types = [typeStr componentsSeparatedByString:@"、"];
        }
       
        if (types.count > 0) {
            //行
            NSInteger nextLineX = 0;
            //列
            NSInteger row = 0;
            for (NSString *type in types) {
                LWTextStorage *tagTextStorage = [[LWTextStorage alloc] init];
                NSString *typeStr = type;
                if ([typeStr isEqualToString:@""]) {
                    break;
                }
                if ([typeStr rangeOfString:@"_"].location == NSNotFound) {
                }else{
                   typeStr = [typeStr substringFromIndex:[typeStr rangeOfString:@"_"].location+1];
                }
                NSString *tagText = [NSString stringWithFormat:@"  %@  ",typeStr];
                tagTextStorage.text = tagText;
                CGFloat textWidth = [self calulateTextHeightWithString:tagText fontSize:10];
                if (textWidth+nextLineX+18 > Screen_Width/3.0*2.0) {
                    row += 1;
                    nextLineX = 0;
                }
                tagTextStorage.frame = CGRectMake(18+nextLineX, deadlineTimeIcon.bottom + 12 + 24*row, textWidth, 16);
                tagTextStorage.textBoundingStrokeColor = [UIColor colorWithHexString:@"#e0e0e0"];
                tagTextStorage.textBackgroundColor = [UIColor colorWithHexString:@"#e0e0e0"];
                tagTextStorage.font = [UIFont systemFontOfSize:10];
                tagTextStorage.textColor = [UIColor colorWithHexString:@"#666666"];
                [self addStorage:tagTextStorage];
                nextLineX += (textWidth+6.0);
            }
        }
        

        self.cellHeight = [self suggestHeightWithBottomMargin:18];
        //如果为历史订单，则显示已抢logo
        if (isHistory) {
            LWImageStorage *historyIcon = [[LWImageStorage alloc] init];
            historyIcon.contents = [UIImage imageNamed:@"已抢-编辑"];
            historyIcon.frame = CGRectMake(Screen_Width - 50, self.cellHeight-50, 50, 50);
            [self addStorage:historyIcon];
        }
    
    }
    return self;
}


//----------------------------------------------------------------------------------//
- (instancetype)initWithModel:(MJProjectListModel *)projectModel
{
    self = [super init];
    if (self) {
        self.projectModel = projectModel;
        //标题
        LWTextStorage *titleStorage = [[LWTextStorage alloc] init];
        titleStorage.frame = CGRectMake(18, 18, Screen_Width/3.0*2.0, 15);
        titleStorage.text = projectModel.project_name;
        titleStorage.textColor = [UIColor colorWithHexString:@"#333333"];
        titleStorage.font = [UIFont fontWithName:@"Helvetica-Bold"size:15];
        [self addStorage:titleStorage];

        //--------------------------------------------------------------//
        //时间icon
        LWImageStorage *timeIconStorage = [[LWImageStorage alloc] init];
        timeIconStorage.contents = [UIImage imageNamed:@"时间"];
        timeIconStorage.frame = CGRectMake(18, titleStorage.bottom+12, RowHeight, RowHeight);
        [self addStorage:timeIconStorage];
        
        //时间内容
        LWTextStorage *timeContentLabel = [[LWTextStorage alloc] init];
        timeContentLabel.frame = CGRectMake(43, timeIconStorage.top, Screen_Width-61, RowHeight);
        timeContentLabel.text = projectModel.dealline_time;
        timeContentLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        timeContentLabel.font = [UIFont systemFontOfSize:14];
        [self addStorage:timeContentLabel];

        //---------------------------------------------------------------//
        //语言icon
        LWImageStorage *languageIconStorage = [[LWImageStorage alloc] init];
        languageIconStorage.contents = [UIImage imageNamed:@"语言"];
        languageIconStorage.frame = CGRectMake(18, timeIconStorage.bottom+12, timeIconStorage.width, timeIconStorage.height);
        [self addStorage:languageIconStorage];
        

        
        
        //---------------------------------字数统计-------------------------------//
        LWTextStorage *wordCountLabel = [[LWTextStorage alloc] init];
        NSString *wordText = [NSString stringWithFormat:@"%@字",(projectModel.wordcount == nil ? @"0":projectModel.wordcount)];
        CGFloat wordTextWidth = [self calulateTextHeightWithString:wordText fontSize:14];
        wordCountLabel.frame = CGRectMake(Screen_Width-wordTextWidth-18, languageIconStorage.top, wordTextWidth+5, RowHeight);
        wordCountLabel.text = wordText;
        wordCountLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        wordCountLabel.font = [UIFont systemFontOfSize:14];
        [self addStorage:wordCountLabel];
        
        //语言互译内容
        LWTextStorage *languageContentLabel = [[LWTextStorage alloc] init];
        languageContentLabel.frame = CGRectMake(43, languageIconStorage.top, wordCountLabel.left-45, RowHeight);
        languageContentLabel.text = [NSString stringWithFormat:@"%@ - > %@",projectModel.sourcelangname,projectModel.targetlangname];
        languageContentLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        languageContentLabel.font = [UIFont systemFontOfSize:14];
        [self addStorage:languageContentLabel];
        
        self.cellHeight = [self suggestHeightWithBottomMargin:18];
    }
    return self;
}

- (CGFloat)calulateTextHeightWithString:(NSString *)string fontSize:(CGFloat)fontSize {
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    CGRect rect = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 16) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width+10;
}
- (MJ_TaskListResult *)resultModel {
    if (!_resultModel) {
        _resultModel = [[MJ_TaskListResult alloc] init];
    }
    return _resultModel;
}

@end
